package cn.tanglong.pet.controller;

import cn.tanglong.basic.exception.MyException;
import cn.tanglong.basic.util.AjaxResult;
import cn.tanglong.basic.util.LoginContextUtil;
import cn.tanglong.basic.util.PageList;
import cn.tanglong.pet.domain.Pet;
import cn.tanglong.pet.domain.SearchMasterMsg;
import cn.tanglong.pet.query.SearchMasterMsgQuery;
import cn.tanglong.pet.service.ISearchMasterMsgService;
import cn.tanglong.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.loadAll();
    }

    // 寻主
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.publish(smm,logininfo.getId());
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    // 待处理
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.pending(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    // 已处理
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.processed(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    // 提交待处理数据
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handle(pet,logininfo.getId());
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("后端失败！");
        }
    }
}
